/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package intelligenttm.InPutDlg;

import intelligenttm.SourceFile.SavePicControl;
import java.net.URL;
import java.util.ResourceBundle;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Button;
import javafx.scene.control.MenuItem;
import javafx.scene.control.Slider;
import javafx.scene.control.SplitMenuButton;
import javafx.scene.control.TextField;
import javafx.scene.input.KeyEvent;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.AnchorPane;

/**
 * FXML Controller class
 *
 * @author 望舒凌霄
 */
public class SavePicControlDlgController implements Initializable {
    @FXML
    private TextField TextQuality;
    @FXML
    private TextField TextWidth;
    @FXML
    private TextField TextHeight;
    @FXML
    private TextField TextResolution;
    @FXML
    private SplitMenuButton ChooseResolution;
    @FXML
    private Slider SliderQuality;
    @FXML
    private Button btnSure;
    @FXML
    private Button btnCancel;
    @FXML
    private AnchorPane RootPane;
    
    private String Type="像素/英寸";
    private SavePicControl SPC=new SavePicControl();
    /**
     * Initializes the controller class.
     */
    @Override
    public void initialize(URL url, ResourceBundle rb) {
        MenuItem MI1=new MenuItem("像素/英寸");
        MenuItem MI2=new MenuItem("像素/厘米");
        EventHandler E=new EventHandler<ActionEvent>() {
            @Override
            public void handle(ActionEvent event) {
                Type=((MenuItem)event.getTarget()).getText();
                ChooseResolution.setText(Type);
            }
        };
        MI1.setOnAction(E);
        MI2.setOnAction(E);// TODO
        ChooseResolution.getItems().addAll(MI1,MI2);
    }    

    @FXML
    private void OnTextChanged(KeyEvent event) {
        int i=Integer.parseInt(TextQuality.getText());
        
        if(i>100){
            i=100;
            TextQuality.setText(String.valueOf(i));
        }
        else if(i<0){
            i=0;
            TextQuality.setText(String.valueOf(i));
        }
        SliderQuality.setValue(i);
    }

    @FXML
    private void OnChoose(ActionEvent event) {
        
    }

    @FXML
    private void OnDragQuality(MouseEvent event) {
        TextQuality.setText( String.valueOf((int)SliderQuality.getValue()));
    }

    @FXML
    private void OnbtnSure(ActionEvent event) {
        int iq=Integer.parseInt(TextQuality.getText());
        double q=(double)iq/100;
        int w=Integer.parseInt(TextWidth.getText());
        int h=Integer.parseInt(TextHeight.getText());
        double p=Double.valueOf(TextResolution.getText());
        SPC.setQUALITY(q);
        SPC.setWidth(w);
        SPC.setHeight(h);
        switch (Type) {
            case "像素/英寸":
                SPC.setPPI(p);
                SPC.Caculate("PPI");
                break;
            case "像素/厘米":
                SPC.setPPM(p);
                SPC.Caculate("PPM");
                break;
        }
        RootPane.getScene().getWindow().hide();
    }

    @FXML
    private void OnbtnCancel(ActionEvent event) {
        SPC=null;
        RootPane.getScene().getWindow().hide();
    }
    
    
    public SavePicControl getSPC(){
        return SPC;
    }
}
